#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/statvfs.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sched.h>
#include <ctype.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <linux/if.h>

#define DF_FILE  "/tmp/df.txt"

struct df_info {
    char file_system[64];
    char size[16];
    char used[16];
    char available[16];
    char use_percent[16];
    char mounted[32];
};

int xy_usb_detect_mount(void)
{
    FILE * fp;
    char buf[1024] = {0};
    int i = 0;
    char * line = NULL;
    size_t len = 0;
    size_t read = 0;
    struct df_info info;

    if (!system("df -h > /tmp/df.txt")) {
        if ((fp = fopen(DF_FILE, "r")) == NULL) {
            printf("%s failed to open.\n", (char *)DF_FILE);
            return -1;
        }
#if 0
        while (!feof(fp)) {
            fscanf(fp, "%s", buf);
            printf("%s\n", buf);
            i++;
        }
        printf("i = %d\n", i);
#endif

#if 0
        while (( read = getline(&line, &len, fp)) != -1) {
            printf("read = %d line = %s", read, line);
        }
        
        if (line)
            free(line);
#endif
#if 0        
        while (fgets(buf, sizeof buf, fp)) {
            printf("%s", buf);
        }
#endif
//        info.size = "512M";
//        printf("%s\n", info.size);

        while (fgets(buf, sizeof buf, fp)) {
#if 0
            char file[32] = {0};
            char size[32] = {0};
            char used[32] = {0};
            char avai[32] = {0};
            char use[32] = {0};
            char mount[32] = {0};
            
            sscanf(buf, "%s %s %s %s %s %s", file, size, used, avai, use, mount);
            printf("%s   %s   %s   %s   %s   %s\n", file, size, used, avai, use, mount);
#endif
            
            memset(&info, 0, sizeof(struct df_info));
            sscanf(buf, "%s %s %s %s %s %s", info.file_system, info.size,
                             info.used, info.available, info.use_percent, info.mounted);

            printf("%s   %s   %s   %s   %s   %s\n", info.file_system, info.size,
                           info.used, info.available, info.use_percent, info.mounted);
        }

        fclose(fp);
    }

    return 0;
}

void main()
{
    xy_usb_detect_mount();
}
